package com.gx.mvp.base;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.gx.mvp.R;
import com.gx.mvp.rx.RxManager;
import com.gx.mvp.utils.KeyBoardUtil;
import com.gx.mvp.utils.StatusBarUtil;
import com.gx.mvp.widget.CustomLoadDialog;

/**
 * Created by Guoxin on 2019/12/05.
 */
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements View.OnClickListener, BaseView {

    protected Context          mContext;
    protected P                mPresenter;
    protected RxManager        mRxManager;
    private   CustomLoadDialog mLoadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//设置statusbar应用所占的屏幕扩大到全屏，但是最顶上会有背景透明的状态栏，它的文字可能会盖着你的应用的标题栏
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        if (Build.BRAND.equals("vivo") && Build.VERSION.SDK_INT < 23) {

        }
        else {
            StatusBarUtil.setStatusBarColor(this, R.color.white);
        }
        StatusBarUtil.statusBarLightMode(this);

        // 添加活动栈
        ActivityManager.getInstance().addActivity(this);
        super.setContentView(getContentViewId());
        this.mContext = this;
        // 创建数据逻辑处理层
        this.mPresenter = this.createPresenter();
        // 创建 通知管理者
        this.mRxManager = new RxManager();
        // 创建加载框
        this.mLoadingDialog = new CustomLoadDialog(this);
        this.mLoadingDialog.setCanceledOnTouchOutside(false);
        // 初始化 UI
        this.initView();
        // 初始化 数据
        this.initData();
        // 初始化 监听器
        this.initListener();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (this.mRxManager == null) {
            this.mRxManager = new RxManager();
        }
    }

    @Override
    protected void onDestroy() {
        if (mLoadingDialog != null) {
            mLoadingDialog.hide();
            mLoadingDialog.destroy();
            mLoadingDialog = null;
        }
        ActivityManager.getInstance().finishActivity(this);
        if (null != this.mRxManager) {
            this.mRxManager.clear();
            this.mRxManager = null;
        }
        if (null != this.mPresenter) {
            mPresenter.onDestroy();
        }
        super.onDestroy();
    }

    /**
     * 创建presenter
     *
     * @return
     */
    protected abstract P createPresenter();

    /**
     * 获取布局ID
     *
     * @return
     */
    protected abstract int getContentViewId();

    /**
     * 初始化组件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化监听器
     */
    protected abstract void initListener();

    @Override
    public void onClick(View v) {

    }

    @Override
    public void showLoading() {
        if (this.mLoadingDialog != null && !this.mLoadingDialog.isShowing() && !this.isDestroyed()) {
            this.mLoadingDialog.show();
        }
    }

    @Override
    public void hideLoading() {
        if (this.mLoadingDialog != null && this.mLoadingDialog.isShowing() && !this.isDestroyed()) {
            this.mLoadingDialog.hide();
        }
    }

    /**
     * 触摸屏幕时隐藏键盘
     *
     * @param ev
     *
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = this.getCurrentFocus();
            if (KeyBoardUtil.isShouldHideInput(v, ev)) {
                KeyBoardUtil.closeKeyboard(v);
                closeKeyboard();
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        return getWindow().superDispatchTouchEvent(ev) || onTouchEvent(ev);
    }

    protected void closeKeyboard() {

    }
}
